package com.mall.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

@Slf4j
public class RedisUtils {
    private static Logger logger = LoggerFactory.getLogger(RedisUtils.class);

    private static final RedisTemplate<String,Object> redisTemplate = SpringContextUtils.getBean("redisTemplate",RedisTemplate.class);

    private static final StringRedisTemplate stringRedisTemplate = SpringContextUtils.getBean("stringRedisTemplate",StringRedisTemplate.class);

    public static boolean set(String key,Object value){
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        }catch (Exception e){
            logger.error("设置缓存失败"+e.getMessage());
            return false;
        }
    }

    /*
    *
    * */
    public static <T> T get(String key){
        return key==null ? null: (T)redisTemplate.opsForValue().get(key)  ;
    }



    public static boolean expire(String key,long time){
        try {
            redisTemplate.expire(key, time, TimeUnit.SECONDS);
            return true;
        }catch (Exception e){
            logger.error("设置缓存过期时间失败");
            return false;
        }
    }

    public static long getExpire(String key){
        return redisTemplate.getExpire(key,TimeUnit.SECONDS);
    }

    public static boolean set(String key,Object value,long time, TimeUnit timeUnit) {
        try {
            redisTemplate.opsForValue().set(key, value,time,timeUnit);
            return true;
        }catch (Exception e){
            logger.error("设置缓存过期时间失败");
            return false;
        }
    }

    public static Boolean hasKey(String key) {
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            logger.error("redis判断key是否存在错误：", e);
            return false;
        }
    }

    /**
     * 删除缓存
     *
     * @param key 可以传一个值 或多个
     */
    @SuppressWarnings("unchecked")
    public static void del(String... key) {
        if (key != null && key.length > 0) {
            if (key.length == 1) {
                redisTemplate.delete(key[0]);
            } else {
                redisTemplate.delete(Arrays.asList(key));
            }
        }
    }
    /**
     *  递增 ，time>=0
     * */
    public static Long inc(String key,long time){
        if (time<0){
            throw new RuntimeException("传入的缓存递增值必须大于0");
        }
        return stringRedisTemplate.opsForValue().increment(key,time);
    }

    /**
     * 递减
     *
     * @param key   键
     * @param delta 要减少几(小于0)
     * @return
     */
    public static Long decr(String key, long delta) {
        if (delta < 0) {
            throw new RuntimeException("递减因子必须大于0");
        }
        return stringRedisTemplate.opsForValue().increment(key, -delta);
    }


}
